package com.bltec.emergency.controller.mgrController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Role;
import com.bltec.emergency.service.mgrService.RoleService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/roles")
public class RolesController {
    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    /**
     * 获得所有角色
     */
    @RequestMapping("getAllRoles")
    public ResponseJson getRoles() {
        try {
            List<Role> roles = roleService.getAll();
            roles.removeIf(role -> role.getId() == 1);

            return new ResponseJson(Constant.OK, JSON.toJSON(roles));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加角色
     */
    @RequiresPermissions("role:insert")
    @RequestMapping("addRole")
    public ResponseJson addRole(String roleName, Integer level, String roleDes) {
        try {
            Role role = new Role();
            role.setRoleName(roleName);
            role.setLevel(level);
            role.setRoleDes(roleDes);
            role.setEnable(true);
            role.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            role.setCreateTime(DateUtil.dateNow());
            roleService.add(role);
            return new ResponseJson(Constant.OK, "创建角色成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建角色失败" + e.getMessage());
        }
    }

    /**
     * 更新角色
     */
    @RequiresPermissions("role:update")
    @RequestMapping("updateRole")
    public ResponseJson updateUser(String roleName, String roleDes, Long id) {
        try {
            Role role = roleService.getRoleById(id);
            role.setRoleName(roleName);
            role.setRoleDes(roleDes);
            role.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            role.setUpdateTime(DateUtil.dateNow());
            roleService.updateRoleById(role);
            return new ResponseJson(Constant.OK, JSON.toJSON(role));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除角色
     */
    @RequiresPermissions("role:delete")
    @RequestMapping("deleteRole")
    public ResponseJson deleteRole(Long id) {
        if (null != roleService.getRoleById(id)) {
            if (roleService.deleteRoleById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }


}
